﻿import { sendInitMsg,sendGetPath,openDev,unload,configureDevInfo,MicrophoneName,setImageWithBase64,configureRestionInfo,configureVideoFormate,VideoName }    from '@/api/mainH5'
var m_isConnectWS = false;
var m_splitTag = "$*$";
var m_lastMessage = "";
var m_imageDataH = 0;
var m_imageDataW = 0;
var m_imageDataS = 0;
var m_stopWait = false; //同步等待
var m_closed = false; //是否被关闭了
var m_ComparePicBase64 = false; //是否是人脸比对base64
var m_compareShowID = ""; //显示人脸模块base64的控件id
var m_idcardBase64 = false; //是否是身份证图片的base64
var retCapture = 1;
var ocrFile = "";
var ocrType = 0;
//导出base64的变量
export var base64String="";//定义图像字符串
export var  photoType=1;//自定义拍照类型
export var  businessCode="";//业务编码
export var   documentTypeCode="";//业务类型
let ws = "";
export function setPhotoType(val){
   photoType=val;
}
export function setBusinessCode(val){
  businessCode=val;
}
export function setDocumentTypeCode(val){
  documentTypeCode=val;
}

 export    function StartWebSocket(){
	 let url = "ws://localhost:9000/";

	if('WebSocket' in window){
            ws = new WebSocket(url);
    }
    else if('MozWebSocket' in window){
        ws = new MozWebSocket(url);
    }else{
		alert("浏览器版本过低，请升级您的浏览器。\r\n浏览器要求：IE10+/Chrome14+/FireFox7+/Opera11+");
	}
   
	ws.onopen = function()
   {
      m_isConnectWS = true;
	  // unload();
     if(m_closed){
       return;
     }
     //反初始化
     // var jsonObj = {FuncName:'camUnInitCameraLib'};
     // sendWsMessage(jsonObj);
    unload();
	  sendInitMsg();//初始化
	  sendGetPath(); //获取电脑中的路径
	  m_closed = false;
   };
	
	
   ws.onmessage = function (evt) 
   { 
   	
   	
   	if(typeof(evt.data)=="string"){
   		
   		var str = evt.data;
   		
   		if(str.length <= 0){
   			
   			return;
   		}
		
		if(str.indexOf("FileEncodeBase64") >=0){
			
			//var strs= new Array(); 
			//strs=str.split(m_splitTag);
			//var baseStr = strs[2];
			//if(SingleCamera.encodeBase64Tag == 1){

            //    SingleCamera.ShowRealPic(baseStr,true);
            //    var jsonObj = {FuncName:'camDeleteFile',argument:{FilePath:SingleCamera.realPicPath}};
            //    sendWsMessage(jsonObj);

			//}else if(SingleCamera.encodeBase64Tag == 0){

           //     SingleCamera.ShowComparePic(baseStr,true);
           //     SingleCamera.compareBase64 = baseStr;

			//}	
			return;
		}
		
		if(str && str.indexOf(m_splitTag)>=0){
			//视频的每一帧
			var strs= new Array();
			strs=str.split(m_splitTag); 
			setImageWithBase64(strs[1]);
		}else{
			//处理其他请求
			console.log(str);
      handleJsonStrMessage(str);
		}
		
		
   		
   	}
  
 	};
	
   ws.onclose = function()
   { 
      m_isConnectWS = false;
	var myimg = document.getElementById("video");
	myimg.src = "images/load1.gif";
		StartWebSocket();
   };
	
}

export  function sendWsMessage(jsonObj){
	var jsonStr = JSON.stringify(jsonObj);
	ws.send(jsonStr);
}

function  handleJsonStrMessage(str){
  var jsonOBJ = JSON.parse(str);
  var name = jsonOBJ.FuncName;
  var re = jsonOBJ.result;
  //初始化
  if( name == "camInitCameraLib"){

    openDev();


    //var jsonObj = {FuncName:'camInitFace',argument:{}};
    //sendWsMessage(jsonObj);

    //获取设备名
    var jsonObj = {FuncName:'camGetDevName'};
    sendWsMessage(jsonObj);

    //获得麦克风
    /*jsonObj = {FuncName:'camGetAudioDevName'};
        sendWsMessage(jsonObj);*/

    //获得录像格式
    /*	jsonObj = {FuncName:'camGetVideoEncodeName'};
          sendWsMessage(jsonObj);*/

  }
  //打开设备
  else if(name == "camOpenDev"){

    if(re == 0){

      //获取分辨率
      var jsonObj = {FuncName:'camGetResolution'};
      sendWsMessage(jsonObj);
      //获取视频格式
      /*var jsonObj = {FuncName:'camGetMediaTypeName'};
            sendWsMessage(jsonObj);*/
      jsonObj = {FuncName:'camSetImageAutoCrop',argument:{CropType:0}};
      sendWsMessage(jsonObj);

      jsonObj = {FuncName:'camSetLivingBodyState',argument:{bOpen:1}};
      sendWsMessage(jsonObj);

    }else{
      alert("打开失败" + re);
    }

  }
  //获取设备名
  else if(name == "camGetDevName"){

    configureDevInfo(re);

  }
  //视频格式
  else if(name == "camGetMediaTypeName")
  {
    /*	configureVideoFormate(re);*/
    //configureVideoStyle(re);
  }

  //获取分辨率
  else if(name == "camGetResolution"){

    configureRestionInfo(re);
  }
  //设置分辨率
  else if(name == "camSetResolution"){

    if(re !=0){

      alert("设置分辨率失败");
    }
  }
  //拍照
  else if(name == "camCaptureImageFile"){

    if(re != 0){

      alert("拍照失败");
    }
    else
    {
      retCapture = re;

    }


  }
  //自动裁切
  else if(name == "camSetImageAutoCrop"){
    if(re != 0){

      alert("自动裁切失败");
    }
  }
  else if(name == "camZooIn"){
    if(re == 0){

      alert("视频放大成功");
    }
  }
  //Base64
  else if(name == "CaptureEncodeBase64"){
    //base64 返回的值
    base64String=re;
    if(photoType==1){
      alert('身份证');

    }else if(photoType==2){
      alert('办理人');
    }
  }
  //条码识别
  else if(name == "coderRecognizeBarcode"){
    alert(re);
  }
  //连拍
  else if(name == "camStartAutoCapture"){

    if(re == "0"){
      alert("连拍开启成功");
    }

  }
  //停止连拍
  else if(name == "camStopAutoCapture"){

    if(re == "0"){
      alert("停止连拍成功");
      AutoCaptureBack("-1000");
    }

  }
  //连拍回调
  else if(name == "AutoCaptureBack"){

    //if(re == "0"){

    AutoCaptureBack(re);
    //}else {
    //	AutoCaptureBack("-1000");
    //alert("连拍回调出错"+String(re));
    //}

  }
  //选择麦克风
  else if(name == "camGetAudioDevName"){
    MicrophoneName(re);
    //console.log(re);
  }
  //录像格式
  else if (name == "camGetVideoEncodeName"){
    //console.log(re);
    VideoName(re);
  }
  //开始录像
  else if(name == "camStartRecord"){
    if(re == 0) {
      alert("开始录像");
      GetVoice();
    }
    else {
      alert("录像失败");
    }
  }
  //关闭录像
  else if(name == "camStopRecord"){
    if(re == 0) alert("录制成功");
    else alert("录制失败");
  }
  else if (name == "camGetMicrophoneVolumeLevel"){
    ShowVioce(re);
  }
}





	
	

